// request.js
import axios from 'axios'
import { ElMessage } from 'element-plus'

const request = axios.create({
    baseURL: process.env.VUE_APP_BASE_API,
    timeout: 6000,
})

request.interceptors.request.use(
    (config) => {
        console.log('Request sent:', config)

        return config
    },
    (error) => {
        console.error('Request error:', error)

        return Promise.reject(error)
    }
)

request.interceptors.response.use(
    (response) => {
        console.log('Response received:', response)

        return response
    },
    (error) => {
        console.error('Response error:', error)

        if (error.code === 'ECONNABORTED' && error.message.includes('timeout')) {
            ElMessage.error('请求超时，请稍后重试')
        }

        return Promise.reject(error)
    }
)

export default request
